package Presenter;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;
import Models.Supplies;

public class SuppliesPresenter extends AbstractPresenter<Supplies> {
	final String SINGLEITEM = "supplies";
	final String MULTIITEM = "suppliess";
	
	public ArrayList<Supplies> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public Supplies tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public Supplies createItemFromJSONObject(JSONObject jsonSupply){
		Supplies supply = new Supplies();
		try{
			supply.setLeadTime(jsonSupply.getInt("leadTime"));
			supply.setMinQty(jsonSupply.getDouble("minimumQty"));
			supply.setOnHand(jsonSupply.getDouble("supOnHand"));
			supply.setResaleable(jsonSupply.getBoolean("supResalable"));
			supply.setResalePrice(jsonSupply.getDouble("supReSalePrice"));
			supply.setSpokenFor(jsonSupply.getDouble("supSpoken"));
			supply.setSupplyID(jsonSupply.getInt("supID"));
			supply.setSupplyName(jsonSupply.getString("supName"));
			supply.setSupplyNotes(jsonSupply.getString("supNotes"));
			supply.setSupplyNumber(jsonSupply.getString("supNumber"));
			supply.setSupplyOnOrder(jsonSupply.getDouble("supOnOrder"));
			supply.setSupplyPrice(jsonSupply.getDouble("supPrice"));
			supply.setSupplyQtyPerCost(jsonSupply.getDouble("supQtyPer"));
			supply.setSupplyUnits(jsonSupply.getString("supUnits"));
			supply.setWearhouseID(jsonSupply.getInt("supWH"));
			supply.setWholesalePrice(jsonSupply.getDouble("supWholeSalePrice"));
			supply.setTaxExempt(jsonSupply.getBoolean("taxExempt"));
			supply.setVersion(jsonSupply.getInt("version"));
			supply.setAttrition(jsonSupply.getDouble("attrition"));
			if(!jsonSupply.isNull("stockCode")){
				//create stock code if set
				StockCodePresenter scPresenter = new StockCodePresenter();
				JSONObject jsonItem = jsonSupply.getJSONObject("stockCode");
				supply.setStockCode(scPresenter.createItemFromJSONObject(jsonItem));
			} else {
				supply.setStockCode(null);
			}
			return supply;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(Supplies item){
		try{
			JSONObject json = new JSONObject();
			json.put("leadTime", item.getLeadTime());
			json.put("minimumQty", item.getMinQty());
			json.put("supOnHand", item.getOnHand());
			json.put("supResalable", item.isResaleable());
			json.put("supReSalePrice", item.getResalePrice());
			json.put("supSpoken", item.getSpokenFor());
			json.put("supID", item.getSupplyID());
			json.put("supName", item.getSupplyName());
			json.put("supNotes", item.getSupplyNotes());
			json.put("supNumber", item.getSupplyNumber());
			json.put("supOnOrder", item.getSupplyOnOrder());
			json.put("supPrice", item.getSupplyPrice());
			json.put("supQtyPer", item.getSupplyQtyPerCost());
			json.put("supUnits", item.getSupplyUnits());
			json.put("supWH", item.getWearhouseID());
			json.put("supWholeSalePrice", item.getWholesalePrice());
			json.put("taxExempt", item.isTaxExempt());
			json.put("version", item.getVersion());
			json.put("attrition", item.getAttrition());
			if(item.getStockCode()!=null){
				final StockCodePresenter scPresenter = new StockCodePresenter();
				json.put("stockCode", scPresenter.createJSON(item.getStockCode()));
			} else {
				json.put("stockCode", JSONObject.NULL);
			}
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public Supplies getEmptyEntity(){
		Supplies entity = new Supplies();
		return entity;
	}
}
